<?php

/**
 * uniBoard API Forum
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_forum extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Forum ID
	 *
	 * @var integer
	 */
	
	public $forum_id 				= 0;
	
	/**
	 * Forum Parent ID
	 *
	 * @var integer
	 */
	
	public $forum_parent			= -1;
	
	/**
	 * Forum Position
	 *
	 * @var integer
	 */
	
	public $forum_pos 				= 0;
	
	/**
	 * Forum Type
	 * 0 - Category
	 * 1 - Forum
	 * 2 - Redirect
	 * 
	 * @var integer
	 */
	
	public $forum_type 				= 1;
	
	/**
	 * Forum Name
	 *
	 * @var string
	 */
	
	public $forum_name 				= '';
	
	/**
	 * Forum Description (RAW)
	 *
	 * @var string
	 */
	
	public $forum_description_raw	= '';
	
	/**
	 * Forum Description (Pre-parsed)
	 *
	 * @var string
	 */
	
	public $forum_description		= '';
	
	/**
	 * Forum Description Parsing Options
	 *
	 * @var string
	 */
	
	public $forum_description_set	= '111';
	
	/**
	 * Forum URL
	 *
	 * @var string
	 */
	
	public $forum_url				= '';
	
	/**
	 * Forum Redirects Number
	 *
	 * @var integer
	 */
	
	public $forum_redirects			= 0;
	
	/**
	 * Forum Threads
	 *
	 * @var integer
	 */
	
	public $forum_threads			= 0;
	
	/**
	 * Forum Posts
	 *
	 * @var integer
	 */
	
	public $forum_posts				= 0;
	
	/**
	 * Forum Last Topic ID
	 *
	 * @var integer
	 */
	
	public $topic_id				= 0;
	
	/**
	 * Forum Last Topic date
	 *
	 * @var integer
	 */
	
	public $topic_date				= 0;
	
	/**
	 * Forum Last Post Author ID
	 *
	 * @var integer
	 */
	
	public $user_id					= 0;
	
	/**
	 * Forum Last Post Author Name
	 *
	 * @var string
	 */
	
	public $user_name				= '';
	
	/**
	 * Allow posting surveys?
	 *
	 * @var bool
	 */
	
	public $forum_allow_polls		= true;
	
	/**
	 * Allow posting links in teh forum?
	 *
	 * @var bool
	 */
	
	public $forum_allow_links		= true;
	
	/**
	 * Allow Topic Icons
	 *
	 * @var bool
	 */
	
	public $forum_allow_icons		= true;
	
	/**
	 * Posting in forum increase user's counter?
	 *
	 * @var bool
	 */
	
	public $forum_count_posts		= true;
	
	/**
	 * Display forum in parent's subforums list?
	 *
	 * @var bool
	 */
	
	public $forum_display			= true;
	
	/**
	 * Forum is closed?
	 *
	 * @var bool
	 */
	
	public $forum_closed			= false;
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Forum ID
		$this -> forum_id		= (integer) $target_id;
		
		// Autoload If Forum ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'forums
			WHERE forum_id = \'' . $this -> forum_id . '\'
			LIMIT 1');
			
			// Mail found
			if ( $forum_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> forum_parent	 			= $forum_result['forum_parent'];
				$this -> forum_pos	 				= $forum_result['forum_pos'];
				$this -> forum_type	 				= $forum_result['forum_type'];
				$this -> forum_name	 				= $forum_result['forum_name'];
				$this -> forum_description_raw		= $forum_result['forum_description_raw'];
				$this -> forum_description			= $forum_result['forum_description'];
				$this -> forum_description_set		= $forum_result['forum_description_set'];
				$this -> forum_url					= $forum_result['forum_url'];
				$this -> forum_redirects			= $forum_result['forum_redirects'];
				$this -> forum_threads				= $forum_result['forum_threads'];
				$this -> forum_posts				= $forum_result['forum_posts'];
				$this -> topic_id					= $forum_result['topic_id'];
				$this -> topic_date					= $forum_result['topic_date'];
				$this -> user_id					= $forum_result['user_id'];
				$this -> user_name					= $forum_result['user_name'];
				$this -> forum_allow_polls			= $forum_result['forum_allow_polls'];
				$this -> forum_allow_links			= $forum_result['forum_allow_links'];
				$this -> forum_allow_icons			= $forum_result['forum_allow_icons'];
				$this -> forum_count_posts			= $forum_result['forum_count_posts'];
				$this -> forum_display				= $forum_result['forum_display'];
				$this -> forum_closed				= $forum_result['forum_closed'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_forum_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> forum_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'forums');
			
				// Store new Mail id
				$this -> forum_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'forums', 'forum_id = \'' . $this -> forum_id . '\'');
			}
			
			// Kill cache
			class_cache::flushCache( 'forums');
			class_cache::flushCache( 'forums_road_map');
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_parent
	 *
	 * @param integer $new_value
	 */
	
	function setForumParent( $new_value){
		
		// Store in Api
		$this -> forum_parent = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_parent'] = $this -> forum_parent;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_pos
	 *
	 * @param integer $new_value
	 */
	
	function setForumPos( $new_value){
		
		// Store in Api
		$this -> forum_pos = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_pos'] = $this -> forum_pos;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_type
	 *
	 * @param integer $new_value
	 */
	
	function setForumType( $new_value){
		
		// Store in Api
		$this -> forum_type = (integer) $new_value;	
	
		// Fix type
		if ( $this -> forum_type < 0 || $this -> forum_type > 2)
		{
			// Make forum a category
			$this -> forum_type = 0;
		}
		
		// Add to made changes
		$this -> changes['forum_type'] = $this -> forum_type;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_name
	 *
	 * @param string $new_value
	 */
	
	function setForumName( $new_value){
		
		// Name Empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> forum_name = $new_value;	
		
			// Too Long?
			if ( mb_strlen( $new_value) < 255)
			{
				// Add to made changes
				$this -> changes['forum_name'] = $this -> appCore -> strEscape( $this -> forum_name);
			}
			else
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_forum_too_long']);	
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_forum_name_empty']);	
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_description_raw
	 *
	 * @param string $new_value
	 */
	
	function setForumDescriptionRaw( $new_value){
		
		// Store in Api
		$this -> forum_description_raw = $new_value;	
	
		// Add to made changes
		$this -> changes['forum_description_raw'] = $this -> appCore -> strEscape( $this -> forum_description_raw);

	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_description
	 *
	 * @param string $new_value
	 */
	
	function setForumDescription( $new_value){
		
		// Store in Api
		$this -> forum_description = $new_value;	
	
		// Add to made changes
		$this -> changes['forum_description'] = $this -> appCore -> strEscape( $this -> forum_description);

	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_description_set
	 *
	 * @param string $new_value
	 */
	
	function setForumDescriptionSet( $new_value){
		
		// Store in Api
		$this -> forum_description_set = $new_value;	
	
		// Add to made changes
		$this -> changes['forum_description_set'] = $this -> appCore -> strEscape( $this -> forum_description_set);

	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_url
	 *
	 * @param string $new_value
	 */
	
	function setForumUrl( $new_value){
		
		// Store in Api
		$this -> forum_url = $new_value;	
	
		// Too long?
		if ( mb_strlen( $this -> forum_url) < 255)
		{
			// Add to made changes
			$this -> changes['forum_url'] = $this -> appCore -> strEscape( $this -> forum_url);
		}
		else
		{
			// Too long, set error
			$this -> setError( $this -> appCore -> lang['acp_forum_url_too_long']);
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_redirects
	 *
	 * @param integer $new_value
	 */
	
	function setForumRedirects( $new_value){
		
		// Store in Api
		$this -> forum_redirects = (integer) $new_value;	
	
		// Ensure not less than zero
		if ( $this -> forum_redirects < 0)
		{
			$this -> forum_redirects = 0;
		}
		
		// Add to made changes
		$this -> changes['forum_redirects'] = $this -> forum_redirects;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_threads
	 *
	 * @param integer $new_value
	 */
	
	function setForumThreads( $new_value){
		
		// Store in Api
		$this -> forum_threads = (integer) $new_value;	
	
		// Ensure not less than zero
		if ( $this -> forum_threads < 0)
		{
			$this -> forum_threads = 0;
		}
		
		// Add to made changes
		$this -> changes['forum_threads'] = $this -> forum_threads;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_posts
	 *
	 * @param integer $new_value
	 */
	
	function setForumPosts( $new_value){
		
		// Store in Api
		$this -> forum_posts = (integer) $new_value;	
	
		// Ensure not less than zero
		if ( $this -> forum_posts < 0)
		{
			$this -> forum_posts = 0;
		}
		
		// Add to made changes
		$this -> changes['forum_posts'] = $this -> forum_posts;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set topic_id
	 *
	 * @param integer $new_value
	 */
	
	function setTopicId( $new_value){
		
		// Store in Api
		$this -> topic_id = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['topic_id'] = $this -> topic_id;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set topic_date
	 *
	 * @param integer $new_value
	 */
	
	function setTopicDate( $new_value){
		
		// Store in Api
		$this -> topic_date = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['topic_date'] = $this -> topic_date;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_id
	 *
	 * @param integer $new_value
	 */
	
	function setUserId( $new_value){
		
		// Store in Api
		$this -> user_id = (integer) $new_value;	
	
		// Add to made changes
		$this -> changes['user_id'] = $this -> user_id;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user_name
	 *
	 * @param string $new_value
	 */
	
	function setUserName( $new_value){
		
		// Store in Api
		$this -> user_name = $new_value;	
	
		// Add to made changes
		$this -> changes['user_name'] = $this -> appCore -> db -> strEscape( $this -> forum_display);
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_allow_polls
	 *
	 * @param bool $new_value
	 */
	
	function setForumAllowPolls( $new_value){
		
		// Store in Api
		$this -> forum_allow_polls = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_allow_polls'] = $this -> forum_allow_polls;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_allow_links
	 *
	 * @param bool $new_value
	 */
	
	function setForumAllowLinks( $new_value){
		
		// Store in Api
		$this -> forum_allow_links = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_allow_links'] = $this -> forum_allow_links;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_allow_icons
	 *
	 * @param bool $new_value
	 */
	
	function setForumAllowIcons( $new_value){
		
		// Store in Api
		$this -> forum_allow_icons = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_allow_icons'] = $this -> forum_allow_icons;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_count_posts
	 *
	 * @param bool $new_value
	 */
	
	function setForumCountPosts( $new_value){
		
		// Store in Api
		$this -> forum_count_posts = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_count_posts'] = $this -> forum_count_posts;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_display
	 *
	 * @param bool $new_value
	 */
	
	function setForumDisplay( $new_value){
		
		// Store in Api
		$this -> forum_display = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_display'] = $this -> forum_display;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set forum_closed
	 *
	 * @param bool $new_value
	 */
	
	function setForumClosed( $new_value){
		
		// Store in Api
		$this -> forum_closed = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['forum_closed'] = $this -> forum_closed;
	
	}
		
}